/*
 * Copyright 2011 Drew Wilder-Goodwin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
#include "resources.h"
#include "utility.h"
#include "globals.h"
#include <wchar.h>

LONG lstrtolW(const WCHAR * str, WCHAR ** end, int base)
{
    INT length;
    LONG value;

    CHAR *endA;

    /* Check that input is not null */
    if (str == NULL) {
        return 0;
    }

    /* Convert to multibyte */
    length =
        WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL,
                            NULL)+1;
    {
        /* Allocate on the stack */
        CHAR strA[length+1];
        if (length <= 0) {
            if (end != NULL) {
                *end = (WCHAR *) str;
                return 0;
            }
        } else {
            WideCharToMultiByte(CP_ACP, 0, str, -1, strA, length+1, NULL,
                                        NULL);
        }

        /* Convert to long */
        value = strtol(strA, &endA, base);
        if (end != NULL) {
            *end =
                (WCHAR *) ((((endA - strA) / sizeof(CHAR)) * sizeof(WCHAR)) +
                           str);
        }
        return value;
    }
}

void messageboxw_resource(HWND hwnd, int textId, int captionId, UINT type)
{
    WCHAR *text;
    WCHAR *caption;
    LoadStringW(g_hinstance, textId, (WCHAR*)&text, 0);
    LoadStringW(g_hinstance, captionId, (WCHAR*)&caption, 0);
    MessageBoxW(hwnd, text, caption, type);
}

VOID wine_fixme_resource(int resource)
{
    WCHAR *text;
    LoadStringW(g_hinstance, resource, (WCHAR*)&text, 0);
    wine_fixme(text);
}

VOID wine_fixme(WCHAR * s)
{
    WCHAR *title;
    LoadStringW(g_hinstance, STRING_COMMON_WINE_FIXME, (WCHAR*)&title,
                0);
    MessageBoxW(g_hwndmain, s, title, MB_ICONEXCLAMATION | MB_APPLMODAL);
}

WCHAR *get_loadwstring_copy(int id)
{
    int length;
    WCHAR *text;
    WCHAR *copy;

    /* Load the string */
    length = LoadStringW(g_hinstance, id, (WCHAR*)&text, 0);
    if (length <= 0)
        return NULL;

    /* Copy the string */
    copy = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
    lstrcpyW(copy, text);
    return copy;
}

const WCHAR *get_resource_string(int id) {
    WCHAR *text;
    LoadStringW(g_hinstance, id, (WCHAR*)&text, 0);
    return text;
}

const WCHAR *get_safe_string(WCHAR * str)
{
    if (str == NULL) {
        return get_resource_string(STRING_COMMON_);
    } else {
        return str;
    }
}

BOOL rects_equal(RECT * left, RECT * right)
{
    if (left->bottom != right->bottom || left->top != right->top
        || left->left != right->left || left->right != right->right)
        return FALSE;

    else
        return TRUE;
}

VOID set_control_text(HWND hDialog, WCHAR * text, int controlId)
{
    HANDLE handle;

    /* Do not send null text */
    if (text == NULL) {
        return;
    }

    /* Get handle and set text */
    handle = GetDlgItem(hDialog, controlId);
    SendMessageW(handle, WM_SETTEXT, 0, (LPARAM) text);
}

VOID set_control_text_resource(HWND hDialog, UINT stringId, int controlId)
{
    HANDLE handle;
    WCHAR *text;
    LoadStringW(g_hinstance, stringId, (WCHAR*)&text, 0);
    handle = GetDlgItem(hDialog, controlId);
    SendMessageW(handle, WM_SETTEXT, 0, (LPARAM) text);
}

VOID insert_tab_resource(HWND hwnd, int stringid, TCITEMW* tie, int tabindex) {
    WCHAR *text;
    LoadStringW(g_hinstance, stringid, (WCHAR*)&text, 0);
    tie->pszText = text;
    SendMessageW(hwnd, TCM_INSERTITEMW, tabindex, (LPARAM)tie);
}
